<?php   
/*\
 *	
 *
 *
\*/
 
# interface function   
function oh_function_framework($m) {
    return oh_this();        
}

# main class of module
class oh_class_framework {
	
	# $prog contain parsed copy of *.oh file called by index(.) or admin(.)
	var $prog = "";  
	
	# $page contain a selected page pased on url GET parameter passed
	var $page = "";
	
	# $hook contain a key of page selected
	var $hook = "";
	
	# $aurl 
	var $aurl = "http://localhost/oh-php-framework/2011/criminal/oh.framework/modules/framework/";
	
	# $item is the ID of selected object actually managed
	var $item = 0;

	# $item is the ID of selected object actually managed
	var $mods = array("oh-edit");
	
	var $flag = array();
	
	# constructor
	function oh_class_framework() {
	}
	
	# call for rendering application into *.oh file
	function debug($f) {
		$this->flag["debug"] = $f;
		return oh_this();
	}
	
	# call for rendering application into *.oh file
	function index($app) {
		$this->prog = $this->load($app);				
		require_once(dirname(__FILE__)."/index.php");		
	}
	
	# call initializzation
	function process_auth() {
		if ( isset($this->prog["auth"]) ) {
			if ( $this->get_value($this->prog,"auth") == "login" ) {
				if ($this->page == "oh-logout") {
					oh("auth")->logout();
					header("Location: index.php");
				}
				if (!oh("auth")->is_logged()) {
					require_once(dirname(__FILE__)."/login.php");
					die();
				}
			}
		}
	}
	
	# call initializzation
	function process_main() {
		if ($this->prog) {
			$this->page = oh_any	( array_merge($this->prog['page'],$this->mods), @$_GET['page'] );
			$this->hook = oh_key_of	( $this->prog['page'],  $this->page   );
			$this->item = isset($_GET['item']) ? (int)$_GET['item'] : 0;
		} else {
			die("No prog");
		}
	}

	function process_lang() {
		global $oh;
		if (file_exists($oh["script"]."/"."en.lang")) {
			$temp = file_get_contents($oh["script"]."/"."en.lang");
			#oh_dump($temp);

			$temp = explode("##",$temp);
			$this->lang = array();
			foreach($temp as $line) {
				$line = explode(":",$line,2);
				$this->lang[trim($line[0])] = isset($line[1]) ? trim($line[1]) : "";
			}
		}
	}
	
	function lang($text) {
		return isset($this->lang[$text]) ? $this->lang[$text] : $text;
	}

	
	# porcess data model prepare class 
	function process_data_model() {
		#oh_dump($this->prog);		
		if ($this->have("datasource")) {		
			oh("dmodel")->connect(
				$this->get_value($this->prog,"datasource.user"),	
				$this->get_value($this->prog,"datasource.pass"),	
				$this->get_value($this->prog,"datasource.name"),	
				$this->get_value($this->prog,"datasource.host")
			);
		}
		if (isset($this->prog["class"])) {
			foreach($this->prog["class"] as $idc=>$class) {	
				$chi = $this->get_children($idc);
				#oh_dump($chi,"class $class");					
				$data = array();
				foreach($chi as $id=>$line) {
					$line = explode(" ",$line);
					$data[$line[0].' as '.$line[1]] = array();
				}				
				oh("dmodel")->new_class($class,$data);
				//oh_dump($data);
			}
		}				
		#$classes = oh("dmodel")->get_class("");
		#oh_dump($classes);
		#oh_debug_file_dump("dmodel.log");
	}
	
	function side($line) {
		$side = 0;
		for($i=0;$i<strlen($line);$i++) {
			if ($line[$i]!="\t") {
				return $side;
			} else {
				$side++;
			}
		}
		return 0;
	}
	
	function process_submit() {
		
	}
	
	function load($app) {
		$code = file_get_contents($app);
		$code = str_replace("\n","\r",$code);
		$code = explode("\r",$code);		
		$type = array();
		$base = 0;
		$idcc = 0;
		$idzz = array(0=>0);
		foreach($code as $id=>$line) {
			$side = $this->side($line);
			$line = trim($line);
			if ($line) {
				$idaa = $id+1;
				$idcc = $idzz[$side];
				//echo "side: $side, base: $base, diff: $diff, idaa: $idaa, idcc: $idcc, line: $line<br/>";
				//var_dump($idzz);
				//echo '<br/>';
				$line = explode(" ",$line." ",2);			
				$type[$line[0]][$idaa.':'.$idcc] = trim($line[1]);
				$idzz[$side+1] = $idaa;			
			}
		}		
		return $type;
	}
	function admin($app) {
		$type = $this->load($app);
		$menu_url="appmenu/";
		require_once(dirname(__FILE__)."/admin.php");		
	}
	function typebox($type,$key) {
		echo '<div class="">';
		echo '<h2>'.$key.'</h2>';
		foreach($type[$key] as $id=>$name) {
			echo '<h3>'.$name.'</h3>';
		}				
		echo '</div>';
	}
	function is_child($a,$b) {
		$a = explode(":",$a);
		$b = explode(":",$b);
		if ($a[1]==$b[0]) {
			return true;
		} else {
			return false;
		}
	}
	function get_children($a,$select=NULL) {
		$ret = array();
		foreach($this->prog as $cmd=>$lines) {			
			foreach($this->prog[$cmd] as $id=>$line) {
				if ($this->is_child($id,$a)) {
					if ($select != NULL) {
						if ($cmd == $select) {
							$ret[$id] = $cmd.' '.$line;
						}
					} else {
						$ret[$id] = $cmd.' '.$line;					
					}
				}
			}
		}
		return $ret;
	}
	function menu_loop(&$pages,&$menu,$k="0:0") {
		foreach($pages as $i => $p) {
			if ($this->is_child($i,$k)) {
				$menu[$p] = array();			
				$this->menu_loop($pages,$menu[$p],$i);
			}
		}
	}
	function menu_draw($menu,$level=0) {
		if ($level==0) {
			$class='class="oh-menu"';
		} else {
			$class='';
		}
		echo '<ul '.$class.'>';
		foreach($menu as $item => $children) {
			echo '<li>';
			echo '<a href="?page='.$item.'">'.$this->lang($item).'</a>';
			if (count($children)>0) {
				$this->menu_draw($children,$level+1);
			}
			echo '</li>';
		}
		echo '</ul>';
	}
	function get_value($type,$path) {
		$path = explode(".",$path);
		foreach($type[$path[0]] as $id1=>$item1) {
			if (count($path)==1) {
				return $item1;
			} else {
				foreach($type[$path[1]] as $id2=>$item2) {
					if ($this->is_child($id2,$id1)) {
						return trim($item2);
					}
				}
			}
		}
	}
	
	function have($command) {
		return isset($this->prog[$command]);
	}
	
	function process_edit_panel($class,$cid) {
		echo '<div class="oh-panel">';
		if ($this->item > 0) {
			$v = oh("dmodel")->get_object("id={$this->item}");
			$c = oh("dmodel")->get_class($v->form);
		} else {
			$c = oh("dmodel")->get_class($class);
			$v = oh("dmodel")->get_object("$class&get=default");
		}		
		
		echo '<h3>'.$this->lang("edit-".$c->name).'</h3>';
		
		//oh_dump($v);
		
		if (oh("simpleform")->onsubmit("oh_edit_".$c->name)) {
			if ($this->item>0) {
				
			} else {
				$data = oh("simpleform")->get_post_data();
				$item = oh("dmodel")->new_object($class,$data);				
			}
			echo $this->lang("oh-saved-".$c->name);
			echo '<a href=""></a>';
		} else {
			$sf = oh("simpleform");
			$sf->open("oh_edit_".$c->name);
			foreach($c->form as $i) {
				$sf->label($this->lang($i['field']));
				$sf->text ($i['field'],$v->{$i['field']});
				$sf->br();
			}
			$sf->label("&nbsp;");
			if ($this->item > 0) {
				$sf->submit("Salva");
			} else {
				$sf->submit("Inserisci");
			}			
			$sf->close();		
		}
		echo '</div>';
	}
	function first_line_in_array($l) {		
		$l = is_array($l) ? array_pop($l) : $l;
		$l = explode(" ",$l);
		$l = array_slice($l,1);
		return $l;
	}
	function process_list_panel($class,$cid) {
		$show = $this->first_line_in_array($this->get_children($cid,"show"));
		$hide = $this->first_line_in_array($this->get_children($cid,"hide"));
		$view = array(
			
		);
		
		$sf = oh("simpleform");
		//oh_dump($view);
		
		echo '<div class="oh-panel">';
		echo '<h3>'.$this->lang("list-".$class).'</h3>';
		echo '<div class="oh-toolbar">';
			echo '<div class="l">';
				$sf->text("search","");
				$sf->submit("Cerca");
			echo '</div>';
			
			echo '<div class="r">';
				$sf->submit("oh-button-delete","Elimina");
			echo '</div>';
			
			echo '<div class="clr"></div>';
		echo '</div>';
		
		$l = oh("dmodel")->get_object($class);
		$c = oh("dmodel")->get_class($class);
		if (count($l)>0) {
			echo '<table border="0" cellspacing="0" cellpadding="0" class="oh-list">';
			echo '<tr class="oh-list-head">';
			echo '<th>'."-".'</th>';	
			echo '<th>'."ID".'</th>';	
			foreach($c->form as $i) {
				echo '<th><a href="">'.$this->lang($i["field"]).'</a></th>';	
			}			
			echo '</tr>';
				
			foreach($l as $i) {				
				if ($i->info->odd) {
					echo '<tr class="oh-list-item">';
				} else {
					echo '<tr class="oh-list-item-odd">';
				}
				echo '<td>';
				$sf->checkbox("rowid_".$i->id);
				echo '</td>';	
				
				echo '<td>'.$this->link("oh-edit",$i->id).'</td>';	
				foreach($c->form as $f) {
					echo '<td>'.$i->{$f["field"]}.'</td>';	
				}							
				echo '<td>';
				//oh_dump($i);
				echo '</td>';
				echo '</tr>';
			}
			echo '</table>';
		}
		echo '</div>';
	}
	
	function link($page,$item) {
		$url 	= '?page='.$page.'&item='.$item;
		$label 	= $item;
		$link 	= '<a href="'.$url.'">'.$label.'</a>';
		return $link;
	}
	
	function process_menu() {
		# prepare and print menu
		$_menu = array();
		$this->menu_loop($this->prog['page'],$_menu);
		$this->menu_draw($_menu);
	}
	function process_page_body() {
		if (false) {
		} else if ($this->page=="oh-edit") {
			$class = "task";
			$this->process_edit_panel($class,0);
		} else {
			# capture "show" childrend of actual "page"
			if (isset($this->prog['show'])&&is_array($this->prog['show'])) {
				foreach($this->prog['show'] as $id => $show) {
					if ($this->is_child($id,$this->hook)) {
						if (function_exists($show)) {
							$show();
						} else {
							echo $this->lang($show);
						}
					}
				}
			}
			# campture "edit" children oh actual "page"
			if (isset($this->prog['edit'])&&is_array($this->prog['edit'])) {
				foreach($this->prog['edit'] as $id => $class) {
					if ($this->is_child($id,$this->hook)) {
						$this->process_edit_panel($class,$id);
					}
				}
			}
			# campture "list" children oh actual "page"
			if (isset($this->prog['list'])&&is_array($this->prog['list'])) {
				foreach($this->prog['list'] as $id => $list) {
					if ($this->is_child($id,$this->hook)) {
						$this->process_list_panel($list,$id);
					}
				}
			}
		}
		echo '<a href="http://localhost/oh-php-framework/2011/criminal/oh.framework/modules/adminer/adminer.php?username=root&db=oh_database&select=oh_class">adminer</a>';
	}
	function process_html_header() {
		require_once("header.php");
		#require_once("style.php");
	}
	function process_html_footer() {
		require_once("footer.php");
	}
}
